const fs = require("fs");
const express = require('express');
const path = require("path");
const app = express();
let router = require('express').Router();
// app.use(express.static('E:/PlayniteHtmlImages_batch/20150130'));
// app.use(express.static('E:/PlayniteHtmlImages_batch/20150131'));

const imageBaseDir = 'E:/PlayniteHtmlImages_batch';
const port = process.env.PORT || 4090;

let map = new Map();
const excludeFolderList = [
  "XYplorer_23.20.0100_Portable",
]
function travel(dir,callback){
  const parentDir = dir.replaceAll("\\", "/").split("/").slice(-1)[0];
  if (excludeFolderList.includes(parentDir)){
    return;
  }
  if (!fs.existsSync(dir)) {
    return;
  }
  fs.readdirSync(dir).forEach((file)=>{
    const pathname=path.join(dir,file)
    // JM: 这里可以做个检查，确保在imageBaseDir下的二级目录是batch目录，否则跳过
    callback(pathname);
  })
}

travel(imageBaseDir, function(pathname){
  app.use(express.static(pathname));
})

var server = app.listen(port, function(){
  console.log('Listening on port ' + server.address().port);
});
